package com.example.shopping2.a_c_fragment.main

import BaseActivity
import IActivity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.AppCompatButton
import com.blankj.utilcode.util.ToastUtils
import com.example.shopping2.MainActivity
import com.example.shopping2.R
import com.example.shopping2.a_a_entity.BaseEntity
import com.example.shopping2.a_a_mvp.contract.HomeContract
import com.example.shopping2.a_a_mvp.model.models.HomeModel
import com.example.shopping2.a_a_mvp.presenter.presenters.HomePresenter
import com.example.shopping2.a_c_fragment.main.entity.greendao.DaoMaster
import com.example.shopping2.a_c_fragment.main.entity.greendao.User
import com.example.shopping2.a_c_fragment.main.entity.greendao.UserManager
import com.example.shopping2.a_c_fragment.main.entity.greendao.UserValue
import com.example.shopping2.a_c_network.HttpCode
import com.example.shopping2.a_c_network.LjlUtils

class LoginActivity : BaseActivity<HomePresenter>(), IActivity, HomeContract.HomeView {
    private val loginReg: TextView by lazy { findViewById<TextView>(R.id.login_reg) }
    private val loginPhone: EditText by lazy { findViewById<EditText>(R.id.login_phone) }
    private val loginPwd: EditText by lazy { findViewById<EditText>(R.id.login_pwd) }
    private val loginButton: AppCompatButton by lazy { findViewById<AppCompatButton>(R.id.login_button) }
    private val logWangji: TextView by lazy { findViewById<TextView>(R.id.log_wangji) }
    private val loginBack: ImageView by lazy { findViewById<ImageView>(R.id.login_back) }
    private var type:String? = null

    override fun findlayout(): Int {
        return R.layout.activity_login
    }

    override fun initView() {
         type = intent.getStringExtra("type")

        //-----------------------------------------关闭页面-------------------------------------
        loginBack.setOnClickListener {
            if (type.equals("goodvalue")){
                finish()
        }
            //发广播
            var intent = Intent()
            intent.setAction("login_finish")

            intent.putExtra("type",type)
            sendBroadcast(intent)
            finish()
        }


        loginReg.setOnClickListener {
            //跳转到注册
            LjlUtils.Ljl_Toast(this, "跳转到注册")
        }
        logWangji.setOnClickListener {
            LjlUtils.Ljl_Toast(this, "跳转忘记密码页面")
        }

    }



    override fun initData() {
        mypre = HomePresenter(HomeModel(), this)

        var list: ArrayList<HashMap<String, Any>> = ArrayList()
        var map: HashMap<String, Any> = HashMap()

        loginButton.setOnClickListener {
            var phone = loginPhone.text.toString().trim()
            var pwd = loginPwd.text.toString().trim()
            map.put("code", HttpCode.LOGIN)
            map.put("mobile", phone)
            map.put("pushId", "")
            map.put("pwd", pwd)
            list.add(map)
            mypre.requestAll(list)
        }

    }

    override fun showHomeData(baseEntity: BaseEntity) {
        if (baseEntity is UserValue) {
            if (baseEntity.status.equals("0")) {
                //成功
                LjlUtils.Ljl_Toast(this, baseEntity.message.toString())

                var data = baseEntity.data
                //数据库
                var userDao = UserManager()
                userDao.setuserdao(this)
                //查询  并存入数据库
                var loadByRowId = userDao.select(data.id)
                if (loadByRowId == null) {
                    userDao.insert(data)
                }
                //改变sp   其他页面判断是否登录了
                getSharedPreferences("loging", MODE_PRIVATE).edit()
                    .putBoolean("log", true)
                    .putLong("id", data.id)
                    .putString("name", data.user_name)
                    .putString("icon", "https://i02piccdn.sogoucdn.com/367da58fe13055a5")
                    .commit()

                //发广播
                var intent = Intent()
                intent.setAction("user_login")
                intent.putExtra("username", data.user_name)
                intent.putExtra("login_dialog", true)
                intent.putExtra("id",data.id)
                intent.putExtra("type",type)
                intent.putExtra("userimg", "https://i02piccdn.sogoucdn.com/367da58fe13055a5")
                sendBroadcast(intent)
                //发送广播 通知页面进行更新   并关闭页面    并存入数据库  并改变sp
                finish()

            } else {
                LjlUtils.Ljl_Toast(this, baseEntity.message.toString())
            }
        }
    }

}